package xian.woniuxy.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xian.woniuxy.entity.Goods;
import xian.woniuxy.service.IGoodsService;
import xian.woniuxy.util.Pageable;
import xian.woniuxy.util.ResultVO;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author Zhuloong
 * @time 2021/12/26 13:52:16
 */
@RestController
@RequestMapping("goods")
public class GoodsController {
    
    @Autowired
    private IGoodsService iGoodsService;

    @GetMapping("/{page}/{size}")
    public ResultVO findAll(@PathVariable int page, @PathVariable int size) {
        Pageable all = iGoodsService.findAll((page - 1) * size, size);
        return new ResultVO(200, "findAll ok", all);
        
    }
    
    @GetMapping("/{goods_name}")
    public ResultVO findFromName(@PathVariable String goods_name) {
        System.out.println("---------");
        List<Goods> list = iGoodsService.findFromName(goods_name);
        return new ResultVO(200, "findFromName ok", list);
        
    }
    @DeleteMapping("/{goods_id}")
    public ResultVO delete(@PathVariable int goods_id) {
        iGoodsService.delete(goods_id);
        return new ResultVO(200, "delete ok", null);
    }

    @PutMapping
    public ResultVO update(@RequestBody Goods goods) {
        iGoodsService.update(goods);
        return new ResultVO(200, "update ok", null);
    }
    @PostMapping
    public ResultVO save(@RequestBody Goods goods){

        System.out.println("goods = " + goods);
        iGoodsService.save(goods);
        return new ResultVO(200, "save ok", goods);
    }
}
